
const formatTime = t => { // 格式化时间  2020-05-05 16:55:28
    const date = new Date(t)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

// 数组去重
const unique = arr => {
    return [...new Set(arr)]
}

// 快速排序 小到大  大到小可用 reverse() 转换
function quickSort (arr, left, right) {
    var len = arr.length
    var partitionIndex
    var left = typeof left !== 'number' ? 0 : left
    var right = typeof right !== 'number' ? len - 1 : right

    if (left < right) {
        partitionIndex = partition(arr, left, right)
        quickSort(arr, left, partitionIndex - 1)
        quickSort(arr, partitionIndex + 1, right)
    }
    return arr
}

function partition (arr, left, right) {     // 分区操作
    var pivot = left                      // 设定基准值（pivot）
    var index = pivot + 1
    for (var i = index; i <= right; i++) {
        if (arr[i] < arr[pivot]) {
            swap(arr, i, index)
            index++
        }
    }
    swap(arr, pivot, index - 1)
    return index - 1
}

function swap (arr, i, j) {
    var temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
}

module.exports = {
    formatTime,
    unique,
    quickSort
}
